# 项目：python_project
# 文件：smoking
# 作者：WuTong
# 日期：2025/5/20,09:50
# coding:utf-8
from time import sleep
from selenium import webdriver
from selenium.webdriver.support.select import Select


def get_driver():
    driver = webdriver.Edge()
    driver.implicitly_wait(5)
    driver.maximize_window()
    return driver

def select_random(element):
    import random
    random_index = random.randint(1,len(Select(element).options)-1)
    Select(element).select_by_index(random_index)

def write(element,value):
    element.click()
    element.clear()
    element.send_keys(value)

if __name__ == '__main__':
    driver = get_driver()
    driver.get('http://localhost:8080/YunDuanBoss/')
    driver.find_element('name','userName').send_keys('YD001')
    driver.find_element('name','userPass').send_keys('YunDuan123')
    driver.find_element('css selector','#form-login > div > div > div.modal-footer > button').click()
    sleep(2)
    driver.get('http://localhost:8080/YunDuanBoss/exercise')
    # 添加一个单选题
    driver.find_element('css selector','#content > div.row.con-margin.con-body-con > '
                        'div.col-lg-12.col-md-12.col-xs-12.con-body-padding.search-blank.text-left > '
                        'div > button:nth-child(1)').click()
    direction = driver.find_element('css selector','#addOption-form > div > div > div:nth-child(3) > '
                                                   'div.col-md-6.col-sm-6.form-group > select')
    select_random(direction)
    stage = driver.find_element('css selector','#addOption-form > div > div > div:nth-child(3) >'
                                               ' div:nth-child(2) > select')
    select_random(stage)
    course = driver.find_element('css selector','#addOption-form > div > div > div:nth-child(4) > '
                                                'div > select')
    select_random(course)
    driver.find_element('id','econtent1').send_keys('这是一个单选题')
    driver.find_element('css selector', '#ul > div:nth-child(2) > input.text').send_keys('这是选项A')
    driver.find_element('css selector', '#ul > div:nth-child(3) > input.text').send_keys('这是选项B')
    driver.find_element('css selector', '#ul > div:nth-child(4) > input.text').send_keys('这是选项C')
    driver.find_element('css selector', '#ul > div:nth-child(5) > input.text').send_keys('这是选项D')
    driver.find_element('css selector', '#ul > div:nth-child(4) > input[type=radio]:nth-child(3)').click()
    driver.find_element('id', 'addOptionBtn').click()
    sleep(5)
    driver.refresh()



    # 添加一个填空题
    driver.find_element('css selector', '#content > div.row.con-margin.con-body-con > '
                                        'div.col-lg-12.col-md-12.col-xs-12.con-body-padding.search-blank.text-left > '
                                        'div > button:nth-child(2)').click()
    direction1 = driver.find_element('css selector','#addBlank-form > div > div > div:nth-child(1) >'
                                                    ' div.col-md-6.col-sm-6.form-group > select')
    select_random(direction1)
    stage1 = driver.find_element('css selector','#addBlank-form > div > div > div:nth-child(1) > '
                                                'div:nth-child(3) > select')
    select_random(stage1)
    course1 = driver.find_element('css selector','#addBlank-form > div > div > div:nth-child(2) > div > select')
    select_random(course1)
    driver.find_element('id','econtent2').send_keys('这是一个填空题')
    driver.find_element('css selector', '#addBlank-form > div > div > div:nth-child(3) > div > label').click()
    driver.find_element('css selector', '#addBlank-form > div > div > '
                                        'div.row.blank-answer-box > div > input').send_keys('这是填空题的答案')
    driver.find_element('css selector', '#addBlank > div > div > div.modal-footer > button').click()
    sleep(5)
    driver.refresh()

    #添加一个简答题
    driver.find_element('css selector', '#content > div.row.con-margin.con-body-con > '
                                        'div.col-lg-12.col-md-12.col-xs-12.con-body-padding.search-blank.text-left > '
                                        'div > button:nth-child(3)').click()
    direction2 = driver.find_element('css selector','#addShort-form > div > div > div:nth-child(1) > '
                                                    'div.col-md-6.col-sm-6.form-group > select')
    select_random(direction2)
    stage2 = driver.find_element('css selector','#addShort-form > div > div > div:nth-child(1) > '
                                                'div:nth-child(3) > select')
    select_random(stage2)
    course2 = driver.find_element('css selector','#addShort-form > div > div > div:nth-child(2) > div > select')
    select_random(course2)
    driver.find_element('id', 'econtent3').send_keys('这是一个简答题')
    driver.find_element('id', 'eanswer3').send_keys('这是简答题的答案')
    driver.find_element('css selector', '#addShort > div > div > div.modal-footer > button').click()
    sleep(5)
    driver.refresh()

    # 随机方向查询题库
    direction3 = driver.find_element('css selector','#content > div.row.con-margin.con-body-con > '
                                                    'div.col-lg-12.col-md-12.col-xs-12.con-body-padding.search-blank.'
                                                    'text-left > select.sel-text.stu-orientation')
    select_random(direction3)
    sleep(3)
    driver.refresh()

    # 随机阶段查询题库
    stage3 = driver.find_element('css selector','#content > div.row.con-margin.con-body-con > '
                                                'div.col-lg-12.col-md-12.col-xs-12.con-body-padding.search-blank.'
                                                'text-left > select:nth-child(2)')
    select_random(stage3)
    sleep(3)
    driver.refresh()

    # 随机课程查询题库
    course3 = driver.find_element('css selector','#content > div.row.con-margin.con-body-con > '
                                                 'div.col-lg-12.col-md-12.col-xs-12.con-body-padding.search-blank.'
                                                 'text-left > select:nth-child(3)')
    select_random(course3)
    sleep(3)
    driver.refresh()

    # 修改单选题
    driver.find_element('css selector', '#exercise-table > tbody > tr:nth-child(3) >'
                                        ' td:nth-child(9) > button').click()
    direction4 = driver.find_element('css selector','#addOption-form > div > div > div:nth-child(3) > '
                                                    'div.col-md-6.col-sm-6.form-group > select')
    select_random(direction4)
    sleep(1)
    stage4 = driver.find_element('css selector','#addOption-form > div > div > div:nth-child(3) > '
                                                'div:nth-child(2) > select')
    select_random(stage4)
    sleep(1)
    course4 = driver.find_element('css selector','#addOption-form > div > div > div:nth-child(4) > div > select')
    select_random(course4)
    sleep(1)
    new_question = driver.find_element('id', 'econtent1')
    write(new_question,'这是新的题干')
    sleep(1)
    # 添加&删除一个选项
    driver.find_element('css selector', '#ul > div:nth-child(1) > span.glyphicon.glyphicon-plus-sign').click()
    driver.find_element('css selector', '#ul > div:nth-child(6) > input.text').send_keys('这是选项E')
    driver.find_element('css selector', '#ul > div:nth-child(1) > span.glyphicon.glyphicon-plus-sign').click()
    driver.find_element('css selector', '#ul > div:nth-child(7) > input.text').send_keys('这是选项F')
    # 修改正确选项
    driver.find_element('css selector', '#ul > div:nth-child(3) > input[type=radio]:nth-child(3)').click()
    # 删除
    driver.find_element('css selector', '#ul > div:nth-child(1) > span.glyphicon.glyphicon-minus-sign').click()
    driver.find_element('id', 'addOptionBtn').click()
    sleep(5)
    driver.refresh()














